import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.util.UUID;

public class DvdManager {
    public Dvd[] dvds = new Dvd[100];
    private Date date;
    private double totalMoney = 0;//计算总金额
    public SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    public void menu() {
        int choice = 0;
        init();//初始化
        do {
            Scanner scanner = new Scanner(System.in);
            functionMenu();//功能菜单
            System.out.println("请输入你的选择：");
            choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    //打印dvd的信息
                    print_dvdArray(dvds);
                    break;
                case 2:
                    //上架
                    upLine(scanner);
                    break;
                case 3:
                    //下架
                    downLine(scanner);
                    break;
                case 4:
                    //出租
                    borrowDvd(scanner);
                    break;
                case 5:
                    //归还
                    returnDvd(scanner);
                    break;
                case 6:
                    findMenu(scanner);//查找菜单
                    break;
                case 7:
                    sortMenu(scanner);
                    break;
                case 0:
                    System.out.println("你选择了退出\n");
                    break;
                default:
                    System.out.println("选择错误，请重新选择\n");
            }
        } while (choice != 0);
    }

    /**
     * 功能菜单
     */
    private void functionMenu() {
        System.out.println("******* DVD出租管理信息系统 *******\n");
        System.out.println("1.\t\t显示");
        System.out.println("2.\t\t上架");
        System.out.println("3.\t\t下架");
        System.out.println("4.\t\t出租");
        System.out.println("5.\t\t归还");
        System.out.println("6.\t\t查找");
        System.out.println("7.\t\t排序");
        System.out.println("0.\t\t退出");
        System.out.println("********************************\n");
    }

    /**
     * 初始化方法　－－－　　１）　　实例化三张DVD　，放入容器（dvds）
     */
    public void init() {
        Dvd dvd = new Dvd();
        dvd.Id = UUID.randomUUID().toString().replace("-", "");
        dvd.name = "战狼";
        dvd.cost = 12;
        date = new Date();
        dvd.upLoadDate = simpleDateFormat.format(date);
        dvd.borrowDate = " ";
        dvd.returnDate = " ";
        dvd.status = DvdStatus.INSTORE;
        dvd.times = 0;
        dvd.borrowMoney = 0.0;
        dvd.index = 0;

        dvds[0] = dvd;
    }

    /**
     * 判断dvds中是否存满DVD
     */
    private int isFull(Dvd[] dvds) {
        for (Dvd dvd : dvds) {
            if (dvd == null) {
                return 1;
            }
        }
        return 0;
    }

    /**
     * 上架
     */
    private void upLine(Scanner scanner) {
        //判断dvds中是不是已满
        if (isFull(dvds) == 0) {
            System.out.println("DVD已满~");
            return;
        }

        int index = getFreePosition();//查找空闲位置
        Dvd dvd = new Dvd();
        //编号
        dvd.Id = UUID.randomUUID().toString().replace("-", "");
        System.out.println("请输入上架电影名称：");
        dvd.name = scanner.next();
        dvd.status = DvdStatus.INSTORE;
        System.out.println("请输入日租金单价：");
        dvd.cost = scanner.nextDouble();
        dvd.index = index;
        //上架时间
        date = new Date();
        dvd.upLoadDate = (simpleDateFormat.format(date));
        dvd.borrowDate = " ";
        dvd.returnDate = " ";
        dvds[index] = dvd;
        print_dvdArray(dvds);
    }

    /**
     * 下架
     */
    private void downLine(Scanner scanner) {

        System.out.println("请输入你要下架的商品名：");
        String name = scanner.next();
        int index = search(name);
        if (index == -1) {
            System.out.println("找不到你想要下架的商品~");
            return;
        }
        dvds[index] = null;
        System.out.println("下架成功~");
    }

    /**
     * 出租
     */
    private void borrowDvd(Scanner scanner) {
        System.out.println("请输入你要出租的DVD名字：");
        String name = scanner.next();
        int index = search(name);
        if (index == -1 || dvds[index].status == DvdStatus.OUTSTORE) {
            System.out.println("没有这张DVD或该DVD已经出租，请重新选择~");
            return;
        }

        date = new Date();
        dvds[index].borrowDate = simpleDateFormat.format(date);//出租时间
        dvds[index].returnDate = " ";
        dvds[index].status = DvdStatus.OUTSTORE;//出租状态
        dvds[index].times++;//出租次数++
        dvds[index].borrowMoney += dvds[index].cost;//计算每个DVD的出租总金额
        System.out.println("出租成功~");
    }

    /**
     * 归还
     */
    private void returnDvd(Scanner scanner) {
        System.out.println("请输入你要要归还的DVD名字：");
        String name = scanner.next();
        int index = search(name);
        if (index == -1 || dvds[index].status != DvdStatus.OUTSTORE) {
            System.out.println("你没有借出去的DVD~");
            return;
        }
        dvds[index].status = DvdStatus.INSTORE;//归还
        date = new Date();
        dvds[index].returnDate = simpleDateFormat.format(date);//归还时间
        System.out.println("归还成功~");
    }

    /**
     * 重载各种参数的查找方法
     */

    /**
     * 查找菜单
     */
    private void findMenu(Scanner scanner) {
        if (dvds.length <= 0) {
            System.out.println("数组中无内容~");
            return;
        }

        int searchChoice = 0;
        String name = "";
        int times = 0;
        int index = -1;
        do {
            System.out.println("*******  查找菜单  *******");
            System.out.println("******* 1.按名称查找 *******");
            System.out.println("******* 2.按热度查找 *******");
            System.out.println("******* 3.按状态查找 *******");
            System.out.println("******* 0.退出 *******");

            System.out.println("请输入你的选择：");
            searchChoice = scanner.nextInt();

            switch (searchChoice) {
                case 1:
                    System.out.println("请输入你要查找的DVD名字：");
                    name = scanner.next();
                    index = search(name);
                    System.out.println(dvds[index]);
                    break;
                case 2:
                    System.out.println("请输入你要查找的热度：");
                    times = scanner.nextInt();
                    for (int i = 0; i < dvds.length && dvds[i] != null; i++) {
                        if (dvds[i].times == times) {
                            System.out.println(dvds[i]);
                        }
                    }
                    break;
                case 3:
                    searchStatus(scanner);//状态查找
                    break;
                case 0:
                    break;
                default:
                    System.out.println("选择错误，请重新选择~");
            }

        } while (searchChoice != 0);
    }

    // 按名称查找
    private int search(String name) {
        if (dvds.length <= 0) {
            System.out.println("数组中无内容~");
            return -1;
        }

        for (int i = 0; i < dvds.length && dvds[i] != null; i++) {
            if (dvds[i].name.equals(name)) {
                return i;//找到了
            }
        }
        return -1;//没有找到
    }

    /**
     * 状态查找的封装方法
     */
    private void searchStatus(Scanner scanner) {
        if (dvds.length <= 0) {
            System.out.println("数组中无内容~");
            return;
        }

        System.out.println("0.在店 1.出租 2.问题碟 3.预定");
        System.out.println("请输入你的选择：");
        int opt = scanner.nextInt();
        switch (opt) {
            case 0:
                for (int i = 0; dvds[i] != null && i < dvds.length; i++) {
                    search(dvds[i], DvdStatus.INSTORE);
                }
                break;
            case 1:
                for (int i = 0; dvds[i] != null && i < dvds.length; i++) {
                    search(dvds[i], DvdStatus.OUTSTORE);
                }
                break;
            case 2:
                for (int i = 0; dvds[i] != null && i < dvds.length; i++) {
                    search(dvds[i], DvdStatus.BADDISK);
                }
                break;
            case 3:
                for (int i = 0; dvds[i] != null && i < dvds.length; i++) {
                    search(dvds[i], DvdStatus.SUBSCRIPT);
                }
                break;
            default:
                System.out.println("选择错误，退出~");
        }
    }

    // 按状态查找
    private void search(Dvd dvd, DvdStatus status) {
        if (dvds.length <= 0) {
            System.out.println("数组中无内容~");
            return;
        }

        if (dvd.status == status) {
            System.out.println(dvd);
        }
    }

    // 按编号查找
    //Dvd searchById(String Id) {
    //    return null;
    //}

    //找出数组空闲位置
    int getFreePosition() {
        int position = -1;
        for (int i = 0; i < dvds.length; i++) {
            if (dvds[i] == null) {
                position = i;
                break;
            }
        }
        return position;
    }

    //租金计算
    private double calculateCost() {
        for (int i = 0; dvds[i] != null && i < dvds.length; i++) {
            totalMoney += dvds[i].borrowMoney;
        }
        return totalMoney;
    }

    //最大边际利润计算
    public double MaxProfit() {
        double max = dvds[0].borrowMoney;
        for (int i = 1; dvds[i] != null && i < dvds.length; i++) {
            if (max < dvds[i].borrowMoney) {
                max = dvds[i].borrowMoney;//找到最大的利润
            }
        }
        return max;
    }


    /**
     * 排序菜单用于使用那种类型去排序DVD数组
     */

    private void sortMenu(Scanner scanner) {
        int choice = 0;
        do {
            System.out.println("1.按名字排序");
            System.out.println("2.按热度排序");
            System.out.println("3.按利润排序");

            System.out.println("请输入你要进行排序的方式：");
            choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    //按名字排序
                    sortByName();
                    break;
                case 2:
                    //热度排序
                    sortByTimes();
                    break;
                case 3:
                    //利润排序
                    sortByProfit();
                    break;
                case 0:
                    break;
                default:
                    System.out.println("选择错误，请重新选择~");
                    break;
            }

        } while (choice != 0);
    }

    //按名字排序
    void sortByName() {
        if(dvds.length <= 0) {
            System.out.println("数组中无元素，无法排序~");
        }
        for (int i = 0; dvds[i] != null && i < dvds.length - 1; i++) {
            int flag = 1;
            for (int j = 0; j < dvds.length - i - 1; j++) {
                if (dvds[j+1] != null && dvds[j].name.length() > dvds[j + 1].name.length()) {
                    flag = 0;
                    Dvd dvd = dvds[j];
                    dvds[j] = dvds[j + 1];
                    dvds[j + 1] = dvd;
                }
            }
            if (flag == 1)
                break;
        }
        System.out.println("排序成功");
        print_dvdArray(dvds);
    }

    //热度排序
    private void sortByTimes() {
        if(dvds.length <= 0) {
            System.out.println("数组中无元素，无法排序~");
        }
        for (int i = 0; dvds[i] != null && i < dvds.length - 1; i++) {
            int flag = 1;
            for (int j = 0; j < dvds.length - i - 1; j++) {
                if (dvds[j+1] != null && dvds[j].times > dvds[j + 1].times) {
                    flag = 0;
                    Dvd dvd = dvds[j];
                    dvds[j] = dvds[j + 1];
                    dvds[j + 1] = dvd;
                }
            }
            if (flag == 1)
                break;
        }
        System.out.println("排序成功");
        print_dvdArray(dvds);
    }

    //利润排序
    void sortByProfit() {

        if(dvds.length <= 0) {
            System.out.println("数组中无元素，无法排序~");
        }
        for (int i = 0; dvds[i] != null && i < dvds.length - 1; i++) {
            int flag = 1;
            for (int j = 0; j < dvds.length - i - 1; j++) {
                if (dvds[j+1] != null && dvds[j].borrowMoney > dvds[j + 1].borrowMoney) {
                    flag = 0;
                    Dvd dvd = dvds[j];
                    dvds[j] = dvds[j + 1];
                    dvds[j + 1] = dvd;
                }
            }
            if (flag == 1)
                break;
        }
        System.out.println("排序成功");
        print_dvdArray(dvds);
    }

    /**
     * 打印DVD数组信息
     */
    void print_dvdArray(Dvd[] arr) {
        for (Dvd dvd : arr) {
            if (dvd != null) {
                System.out.println(dvd);
            }
        }
    }

    //返回总金额
    public double getTotalMoney() {
        return calculateCost();//调用计算总金额的方法
    }
}